package server;


import SBLib.File;
import SBLib.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * - Entry point of Server.
 * - Listens on a hard-coded socket for connections, and when receives them
 *      it delegates the work to a ServerWorker which will run in it's own thread.
 * 
 * 
 * @author BrandonNeal
 */
public class Server {

    public static final String SPLIT = "&&&"; //use this all over the place from now on TODO
    public static int ServerPort = 9997;
    public static int ReplicatorPort = 9998;
    public static int PingPort = 9996;
    
    public static Boolean STUPID_MODE=false;
    
    final ArrayList<File> Files = new ArrayList<File>();
    final ArrayList<User> Users = new ArrayList<User>();
       
    public String CurrentRM;
    public Replicator replicator;
    
    public static void main(String[] args) 
    {
        Server server = new Server();
        
        server.LoadServer();
        server.RunServer();
        
    }
   
    public void RunServer()
    {
        
        //replicator = new Replicator(this.Files,this.Users);
        //ReplicatorServer replicatorServer = new ReplicatorServer(this.Files, this.Users, replicator);
        //PingListener pl = new PingListener();
        try 
        {
            ServerSocket listenSocket = new ServerSocket(Server.ServerPort);
            while(true)
            {
                Socket clientSocket = listenSocket.accept(); //Listen for connections
                //ObjectInputStream i = new ObjectInputStream(clientSocket.getInputStream());
                //ObjectOutputStream o = new ObjectOutputStream(clientSocket.getOutputStream());
                System.out.println("Connection Made");
                ServerWorker worker = new ServerWorker(clientSocket, this.Files, this.Users); //Passes the socket, along with the Files and User lists to ServerWorer thread
            }
        }
        catch (IOException ex) 
        {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     private void LoadServer()
    {
        this.LoadUsers();
        this.LoadFiles();
    }
    private void LoadFiles()
    {
        String[] files = FileIO.GetFiles(FileIO.FILE_DIR);
        for(String filename:files)
        {
            String fileContent = FileIO.GetFileContent(FileIO.FILE_DIR+filename);
            File file = new File(filename, fileContent);
            this.Files.add(file);
            
        }
    }
    private void LoadUsers()
    {
        String[] users = FileIO.GetFiles(FileIO.USER_DIR);
        for(String username:users)
        {
            String password = FileIO.GetFileContent(FileIO.USER_DIR+username);
            User user = new User(username,password);
            this.Users.add(user);
            
        }
    }
}
